//
//  lua_CCArmatureDisplay_manual.cpp
//  zgirl
//
//  Created by xiaoz on 2017/1/3.
//
//

#include "lua_CCArmatureDisplay_manual.hpp"

#include "../dragonBones/CCArmatureDisplay.h"
#include "scripting/lua-bindings/manual/tolua_fix.h"
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
#include "scripting/lua-bindings/manual/CCLuaValue.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"


static int lua_db_CCArmatureDisplay_setAnimationEventListener(lua_State* L)
{

	int argc = 0;
	dragonBones::CCArmatureDisplay* self = nullptr;

#if COCOS2D_DEBUG >= 1
	tolua_Error tolua_err;
	if (!tolua_isusertype(L, 1, "db.CCArmatureDisplay", 0, &tolua_err)) goto tolua_lerror;
#endif

	self = static_cast<dragonBones::CCArmatureDisplay*>(tolua_tousertype(L, 1, 0));

#if COCOS2D_DEBUG >= 1
	if (nullptr == self) {
		tolua_error(L, "invalid 'self' in function 'lua_db_CCArmatureDisplay_setAnimationEventListener'\n", nullptr);
		return 0;
	}
#endif

	argc = lua_gettop(L) - 1;

	if (argc == 1)
	{
#if COCOS2D_DEBUG >= 1
		//参数2 表示栈的第二个位置是funciton, 第一个位置是funciton调用者self
		if (!toluafix_isfunction(L, 2, "LUA_FUNCTION", 0, &tolua_err))
		{
			goto tolua_lerror;
		}
#endif
		LUA_FUNCTION handler = (toluafix_ref_function(L, 2, 0));

		self->setAnimationEventListener
			([=](const std::string& eventType,
			const std::string& armatureName,
			const std::string& animationName)
		{
			LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
			//将参数ref压入栈，拱lua回调函数调用
			stack->pushString(eventType.c_str());
			stack->pushString(armatureName.c_str());
			stack->pushString(animationName.c_str());
			//执行带一个参数的lua脚本，并返回执行结果
			//return stack-> (handler, 3);
		});
		return 0;
	}
	else if (argc == 2)
	{
		Ref* target = static_cast<cocos2d::Ref*>(tolua_tousertype(L, 2, 0));
#if COCOS2D_DEBUG >= 1
		if (!toluafix_isfunction(L, 3, "LUA_FUNCTION", 0, &tolua_err))
		{
			goto tolua_lerror;
		}
#endif
		LUA_FUNCTION handler = (toluafix_ref_function(L, 3, 0));

		self->setAnimationEventListener
			([=](const std::string& eventType,
			const std::string& armatureName,
			const std::string& animationName)
		{
			LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
			//target
			//             std::string typeName = typeid(*target).name();
			//             auto iter = g_luaType.find(typeName);
			//             toluafix_pushusertype_ccobject(L, target->_ID, &target->_luaID, (void*)target, iter->second.c_str());
			toluafix_pushref(L, (void*)target);
			stack->pushString(eventType.c_str());
			stack->pushString(armatureName.c_str());
			stack->pushString(animationName.c_str());
			//执行带一个参数的lua脚本，并返回执行结果
			//return stack->executeFunctionByHandler(handler, 4);
		});
		return 0;
	}
	luaL_error(L, "%s has wrong number of arguments: %d, was expecting %d\n ", "db.CCArmatureDisplay:setAnimationEventListener", argc, 1);
	return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
	tolua_error(L, "#ferror in function 'lua_db_CCArmatureDisplay_setAnimationEventListener'.", &tolua_err);
#endif
	return 0;
}

static int lua_db_CCArmatureDisplay_setFrameEventListener(lua_State* L)
{

	int argc = 0;
	dragonBones::CCArmatureDisplay* self = nullptr;

#if COCOS2D_DEBUG >= 1
	tolua_Error tolua_err;
	if (!tolua_isusertype(L, 1, "db.CCArmatureDisplay", 0, &tolua_err)) goto tolua_lerror;
#endif

	self = static_cast<dragonBones::CCArmatureDisplay*>(tolua_tousertype(L, 1, 0));

#if COCOS2D_DEBUG >= 1
	if (nullptr == self) {
		tolua_error(L, "invalid 'self' in function 'lua_db_CCArmatureDisplay_setFrameEventListener'\n", nullptr);
		return 0;
	}
#endif

	argc = lua_gettop(L) - 1;

	if (argc == 1)
	{
#if COCOS2D_DEBUG >= 1
		//参数2 表示栈的第二个位置是funciton, 第一个位置是funciton调用者self
		if (!toluafix_isfunction(L, 2, "LUA_FUNCTION", 0, &tolua_err))
		{
			goto tolua_lerror;
		}
#endif
		LUA_FUNCTION handler = (toluafix_ref_function(L, 2, 0));

		self->setFrameEventListener([=](const std::string& eventName,const std::string& armatureName,const std::string& animationName)
		{
			LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
			//将参数ref压入栈，拱lua回调函数调用
			stack->pushString(eventName.c_str());
			stack->pushString(armatureName.c_str());
			stack->pushString(animationName.c_str());
			//执行带一个参数的lua脚本，并返回执行结果
			//return stack->executeFunctionByHandler(handler, 3);
		});
		return 0;
	}
	else if (argc == 2)
	{
		Ref* target = static_cast<cocos2d::Ref*>(tolua_tousertype(L, 2, 0));
#if COCOS2D_DEBUG >= 1
		if (!toluafix_isfunction(L, 3, "LUA_FUNCTION", 0, &tolua_err))
		{
			goto tolua_lerror;
		}
#endif
		LUA_FUNCTION handler = (toluafix_ref_function(L, 3, 0));

		self->setFrameEventListener([=](const std::string& eventName,const std::string& armatureName,const std::string& animationName)
		{
			LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
			//target
			toluafix_pushref(L, (void*)target);
			//             std::string typeName = typeid(*target).name();
			//             auto iter = g_luaType.find(typeName);
			//             toluafix_pushusertype_ccobject(L, target->_ID, &target->_luaID, (void*)target, iter->second.c_str());
			stack->pushString(eventName.c_str());
			stack->pushString(armatureName.c_str());
			stack->pushString(animationName.c_str());
			//执行带一个参数的lua脚本，并返回执行结果
			//return stack->executeFunctionByHandler(handler, 4);
		});
		return 0;
	}

	luaL_error(L, "%s has wrong number of arguments: %d, was expecting %d\n ", "db.CCArmatureDisplay:setFrameEventListener", argc, 1);
	return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
	tolua_error(L, "#ferror in function 'lua_db_CCArmatureDisplay_setFrameEventListener'.", &tolua_err);
#endif
	return 0;
}


int register_xiaoz_CCArmatureDisplay_manual(lua_State* L)
{
	if (nullptr == L)
		return 0;

	lua_pushstring(L, "db.CCArmatureDisplay");
	lua_rawget(L, LUA_REGISTRYINDEX);
	if (lua_istable(L, -1))
	{
		tolua_function(L, "setAnimationEventListener", lua_db_CCArmatureDisplay_setAnimationEventListener);
		tolua_function(L, "setFrameEventListener", lua_db_CCArmatureDisplay_setFrameEventListener);
	}
	lua_pop(L, 1);

	return 0;
}
